package leetcode.medium.q238;

import java.util.HashSet;
import java.util.Set;

public class Main {

}

class Solution {
    public int[] productExceptSelf(int[] nums) {
        int[] answer = new int[nums.length];
        Set<Integer> zeroIndex = new HashSet<>();
        Integer multi = null;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                if (multi != null) multi *= nums[i];
                else multi = nums[i];
            } else {
                zeroIndex.add(i);
                if (zeroIndex.size()>1) break;
            }
        }
        if (zeroIndex.isEmpty()) {
            for (int i = 0; i < nums.length; i++) {
                answer[i] = multi / nums[i];
            }
        } else if (zeroIndex.size() == 1) {
            answer[zeroIndex.stream().findFirst().get()] = multi;
        }
        return answer;
    }
}